const FairyEditor = CS.FairyEditor
const System = CS.System
import { $generic } from 'puerts';
import SG from "./SG";
const App = FairyEditor.App;

export default class ToolMgr {

    /**获取选中目标数量 */
    static getSelectedTargetCount(): number {
        let items = App.activeDoc.inspectingTargets;
        let index = 0;
        try {
            while ((items.get_Item(index) != null)) {
                index++;
            }
        } catch (e) {
            return index
        }
        return index
    }

    /**获取UBB Url文本 */
    static getUBBUrl(url: string, titile: string) {
        return `[url=` + `${url}]${titile}[/url]`
    }

    /**UBB file文本 */
    static getUBBFile(data: CS.FairyEditor.FPackageItem) {

        let a = `[url=${ToolMgr.getFilePath(data)}]`
        let b = this.getAbsPackageItemPath(data)
        let c = "[/url]"
        return a + b + c
    }

    /**获取packageItem的File路径 不带后缀 */
    static getFilePath(data: CS.FairyEditor.FPackageItem) {
        return `file://${ToolMgr.getAbsPackageItemPath(data)}${ToolMgr.getAssetType(data)}`
    }

    /**获取packageItem的绝对路径 不带后缀 */
    static getAbsPackageItemPath(data: CS.FairyEditor.FPackageItem) {
        return `${App.project.basePath}\\assets\\${data.owner.name}${data.path}${data.name}`
    }

    /**获取资源类型的后缀名 */
    static getAssetType(asset: CS.FairyEditor.FPackageItem) {
        switch (asset.type) {
            case FairyEditor.FPackageItemType.IMAGE:
                return ".png";
            case FairyEditor.FPackageItemType.ATLAS:
                return ".jpg";
            case FairyEditor.FPackageItemType.SPINE:
                return ".json"
        }
    }

    /**拼接fgui url */
    static joinUrl(pkg: string, res: string) {
        return `ui://${pkg}/${res}`;
    }

    /**创建泛型List */
    static createGenericList<T>(t: any) {
        let List = $generic(System.Collections.Generic.List$1, t);
        let list = new List<T>();
        return list
    }

    /**创建泛型字典 */
    static createGenericDictionary<T, K>(t: any, k: any) {
        let Dictionary = $generic(System.Collections.Generic.Dictionary$2, t, k);
        let dictionary = new Dictionary<T, K>();
        return dictionary;
    }


    //#region IO
    /**加载Json文件 */
    static loadJson(path: string) {
        if (System.IO.File.Exists(path) == false) {
            return null;
        }
        let f = System.IO.File.ReadAllText(path)
        return JSON.parse(f) as {}
    }

    /**保存Json文件 */
    static writerJson(path: string, msg: {} | null) {
        let config = JSON.stringify(msg)
        System.IO.File.WriteAllText(path, config)
    }

    /**写入配置 */
    static saveConfig(path: string) {
        let config = JSON.stringify(SG.config)
        System.IO.File.WriteAllText(path, config)
    }

    //#endregion

}